<template>
    <!--flex弹性盒子模型，justify-content：主抽 -->
    <div style="display: flex; justify-content: center;">
      <div class="background">
        <img :src="imgSrc" width="100%" height="100%" alt="" />
      </div>
      <el-card style="width: 450px; margin: 1%">
        <div slot="header" class="clearfix">
          <span class="login-title">欢迎加入轻听音乐</span>
          <el-row style="float: right">
            <el-button type="text" style="float: right; font-size: 1px;" @click="doLogin">前往登录<i class="el-icon-user-solid el-icon--right"></i></el-button>
          </el-row>
        </div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
          <el-form-item label="登录名" prop="userId">
            <el-input type="text" v-model="ruleForm.userId" placeholder="请输入登录名" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" placeholder="请再次输入密码" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="userName">
            <el-input type="text" v-model="ruleForm.userName" placeholder="请输入昵称" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobilePhone">
            <el-input type="text" placeholder="请输入手机号码" maxlength="11" v-model="ruleForm.mobilePhone" autocomplete="off" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthDay">
            <el-date-picker class="el-left" v-model="ruleForm.birthDay" type="date" placeholder="请选择出生日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model.number="ruleForm.age" placeholder="请输入年龄"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select class="el-left" v-model="ruleForm.sex" filterable clearable size="medium" placeholder="请选择性别">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="住址" prop="address">
            <el-input v-model="ruleForm.address" placeholder="请输入住址" type="text">
              <i class="el-icon-place el-input__icon" slot="suffix" @click="handleIconClick"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="agree">
            <el-checkbox class="el-left1" v-model="ruleForm.agree">我已阅读并同意用户协议</el-checkbox>
          </el-form-item>
          <el-row style="padding-top: 10px;">
            <el-button type="primary" class="sub" @click="doRegister('ruleForm')">提交注册</el-button>
          </el-row>
        </el-form>
      </el-card>
    </div>
</template>

<script>

import Vue from 'vue'
import { Button, Form, FormItem, Input, Message } from 'element-ui'
import { SignUp } from '../../api/index'

Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.prototype.$message = Message
export default {
  name: 'Register',
  data: function () {
    var that = this
    var checkName = function (rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    var validatePass = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validateCheckPass = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== that.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var checkMobile = function (rule, value, callback) {
      var reg = /^1(3|4|5|6|7|8|9)d{9}$/
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!value && !(reg.test(value))) {
        callback(new Error('手机号格式不正确'))
      } else {
        callback()
      }
    }
    var checkAge = function (rule, value, callback) {
      if (!value) {
        return callback(new Error('年龄不能为空'))
      }
      if (!Number.isInteger(value)) {
        callback(new Error('请输入数字值'))
      } else {
        if (value < 12) {
          callback(new Error('必须年满12周岁'))
        } else {
          callback()
        }
      }
    }
    // 自定义表单验证是否勾选协议协议
    var validateAgree = function (rule, value, callback) {
      if (value) {
        callback()
      } else {
        callback(new Error('请勾选同意协议'))
      }
    }
    return {
      ruleForm: {
        password: '',
        userId: '',
        checkPass: '',
        userName: '',
        mobilePhone: '',
        age: '',
        sex: '',
        birthDay: '',
        address: '',
        agree: false
      },
      rules: {
        userId: [
          { validator: checkName, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validateCheckPass, trigger: 'blur' }
        ],
        mobilePhone: [
          { validator: checkMobile, trigger: 'blur' }
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ],
        // 自定义验证是否勾选协议
        agree: [
          { validator: validateAgree, trigger: 'change' }
        ]
      },
      options: [
        {
          value: '1',
          label: '男'
        }, {
          value: '2',
          label: '女'
        }
      ],
      imgSrc: require('../../assets/img/music.jpg'),
      checked: true
    }
  },
  methods: {
    doLogin: function () {
      this.$router.push({path: '/page/login/login'})
    },
    change: function () {
      console.log('checked：' + this.checked)
    },
    handleIconClick: function () {},
    // 注册
    doRegister: function (formName) {
      var that = this
      that.$refs[formName].validate(function (valid) {
        if (valid) {
          var params = {}
          params.userId = that.ruleForm.userId // 登录名
          params.passWord = that.ruleForm.password // 密码
          params.comFirmPassWord = that.ruleForm.checkPass // 确认密码
          params.userName = that.ruleForm.userName // 昵称
          params.mobilePhone = that.ruleForm.mobilePhone // 手机号
          params.age = that.ruleForm.age // 年龄
          params.sex = that.ruleForm.sex // 性别
          params.birthDay = that.ruleForm.birthDay // 生日
          params.address = that.ruleForm.address // 住址
          SignUp(params)
            .then(res => {
              if (res.code === '000000') {
                that.$message({
                  message: '注册成功',
                  type: 'success'
                })
                localStorage.setItem('userId', that.ruleForm.userId)
                that.$router.push({path: '/page/login/login'})
              } else {
                that.$message({
                  message: res.message,
                  type: 'warning'
                })
              }
            })
            .catch(failResponse => {})
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mounted: function () {
    this.ruleForm.userId = localStorage.getItem('userId')
  }
}
</script>

<style scoped>
.background{
    width:40%;
    z-index: -1;
    margin: 1%;
    float: left;
    position: relative;
}
.login-title{
  margin-left: 20%;
  font-weight: bold;
  color: #409EFF;
}
.el-button--primary.sub{
  width: 50%;
}
.el-left{
  margin-left: -31%;
}
.el-left1{
  margin-left: -65%;
}
</style>
